package com.hmusic.mymusic.service.impl;

import com.hmusic.mymusic.dao.RankcMapper;
import com.hmusic.mymusic.domain.Rankc;
import com.hmusic.mymusic.service.RankcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 歌单评分service实现类
 */
@Service
public class RankcServiceImpl implements RankcService {

    @Autowired
    private RankcMapper rankcMapper;

    /**
     * 添加评分
     *
     * @param rankc
     */
    @Override
    public void insert(Rankc rankc) {
        Rankc rankc1 = rankcMapper.isRankc(rankc);
        if (rankc1 != null){
            throw new RuntimeException("您已评分过！");
        }else {
                rankcMapper.insert(rankc);
            }
    }

    /**
     * 查询歌单总分
     *
     * @param song_list_id
     */
    @Override
    public int selectScoreSum(String song_list_id) {
        return rankcMapper.selectScoreSum(song_list_id);
    }

    /**
     * 查询歌单评价人数
     *
     * @param song_list_id
     */
    @Override
    public int selectRankNum(String song_list_id) {
        return rankcMapper.selectRankNum(song_list_id);
    }

    /**
     * 计算平均分
     *
     * @param songListId
     */
    @Override
    public int rankOfSongListId(String songListId) {
        int rankNum = rankcMapper.selectRankNum(songListId);
        if (rankNum == 0){
            return 10;
        }
        return rankcMapper.selectScoreSum(songListId)/rankNum;
    }
}
